 package com.health.system.javacommon.util;

/**
 * author:sfk
 * 完成日常日期转换
 */

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.sql.Timestamp;
import java.util.Date;

public class DateUtil {

	  public static final String FULL_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	  public static final String DATE_PATTERN = "yyyy-MM-dd";
	  public static final String MONTH_PATTERN = "yyyy-MM";
	  public static final String NOT_SEPARATOR_DATE_PATTERN = "yyyyMMdd";
	  
	public static String dateFormat(Date date) {
		String result = null;
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		if (date == null) {
			return "";
		} else {
			result = sd.format(date);
		}
		return result;
	}

	/**
	 * 根据格式获得日期字符串
	 * 
	 * @author bzq add
	 * @param sFormat
	 * @return
	 */
	public static String getDateStr(String sFormat) {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
		String nowTime = sdf.format(now);
		return (nowTime);
	}

	public static Date getDate(String date) {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date result = null;
		try {
			result = sd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}
	
	/**
	 * 字符串转成指定格式日期
	 * @param date
	 * @param format
	 * @return
	 */
	public static Date getDate(String date,String format) {
		SimpleDateFormat sd = new SimpleDateFormat(format);
		Date result = null;
		try {
			result = sd.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return result;
	}

	public static String getDate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String mDateTime = formatter.format(cal.getTime());
		return mDateTime;
	}

	/**
	 * 取得有时分秒的日期(Timestamp)
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return Timestamp
	 */
	public static Timestamp getTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 将Timestamp转化为字符串(yyyy/MM/dd HH:mm:ss)
	 * 
	 * @author pch 2010.7.8
	 * 
	 * @return 日期字符串
	 */
	public static String parseTimestampToStr(Timestamp time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatter.format(time);
		return formatter.format(time);
	}
	
	/**
	 * 时间前推或后推分钟,其中JJ表示分钟.
	 */
    public static String getPreTime(String sj1, String jj) {
	    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String mydate1 = "";
	    try {
	        Date date1 = format.parse(sj1);
	        long Time = (date1.getTime() / 1000) - Integer.parseInt(jj) * 60;
	        date1.setTime(Time * 1000);
	        mydate1 = format.format(date1);
	    } catch (Exception e) {
	    }
	    return mydate1;
	}
    
    /**
     * 返回当前时间 两位年份+两位月份+两位日期+两位小时+两位分钟+两位秒 的字符串
     * @return
     */
    public static String getTimeStr(){
		Date date = new java.util.Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyMMddHHmmss");
		String dateStr = sd.format(date);
		return dateStr;
    }
	public static void main(String[] args) {
		String now = getTimeStr();
		System.out.println(now);
	}
	
	//////kingter.jar
	
	public static String dateToString(String pattern, Date date)
	  {
	    SimpleDateFormat format = new SimpleDateFormat(pattern);
	    return format.format(date);
	  }

	  public static String currenDateToString()
	  {
	    return dateToString("yyyy-MM-dd HH:mm:ss", new Date());
	  }

	  public static String currenDateToString(String pattern)
	  {
	    return dateToString(pattern, new Date());
	  }

	  public static boolean compareDate(String pattern, String sFirstDate, String sSecondlyDate)
	  {
	    Date firstDate = formatDate(pattern, sFirstDate);
	    Date secondlyDate = formatDate(pattern, sSecondlyDate);
	    return firstDate.before(secondlyDate);
	  }

	  public static boolean afterDate(String pattern, String sFirstDate, String sSecondlyDate)
	  {
	    Date firstDate = formatDate(pattern, sFirstDate);
	    Date secondlyDate = formatDate(pattern, sSecondlyDate);
	    return firstDate.after(secondlyDate);
	  }

	  public static Date formatDate(String pattern, String sDate)
	  {
	    Date date = null;
	    DateFormat dateFormat = new SimpleDateFormat(pattern);
	    try {
	      date = dateFormat.parse(sDate);
	    }
	    catch (Exception ex) {
	      ex.printStackTrace();
	    }
	    return date;
	  }

	  public static Date rollDate(Date date, int field, int step)
	  {
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.add(field, step);
	    return calendar.getTime();
	  }

	  public static Date setDate(Date date, int field, int value)
	  {
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    calendar.set(field, value);
	    return calendar.getTime();
	  }

	  public static int getField(int field)
	  {
	    Calendar calendar = Calendar.getInstance();
	    return calendar.get(field);
	  }

	  public static int getField(Date date, int field)
	  {
	    Calendar calendar = Calendar.getInstance();
	    calendar.setTime(date);
	    return calendar.get(field);
	  }

	   
	
}
